/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * @author Administrator
 * Date: 2010-7-6 上午11:09:19
 */
package com.ccxe.manage.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ccxe.manage.dao.AbstractPageDao;
import com.ccxe.manage.model.RoleBean;
import com.ccxe.manage.util.help.DaoHelper;

/**
 * IM_ROLE 表 dao
 * 
 * @author wangyx
 * 
 */
public class RoleDaoImpl extends AbstractPageDao{

	private DaoHelper dHelper = new DaoHelper();
	
	public List<Map<?, ?>> queryAll() throws Exception {
		List<Map<?, ?>> list = dHelper.getListBySql(getQueryAllSql());
		return list;
	}

	public Map<?, ?> queryOneById(String id) throws Exception {
		List<Map<?, ?>> result = dHelper.getListBySql(queryOneByIdSql(),
				new String[] { id });
		return (result != null && result.size() > 0) ? result.get(0) : null;
	}

	/**
	 * values包含ROLE_NAME,DESCRIPTION、ROLE_ID
	 * 
	 * @param values
	 * @return
	 * @throws Exception
	 */
	public int update(Object[] values) throws Exception {
		return dHelper.insertOrUpdateOrDelete(getUpdateSql(), values);
	}

	public int delete(String roleId) throws Exception {
		return dHelper.insertOrUpdateOrDelete(getDeleteSql(),
				new String[] { roleId });
	}

	public int insertOne(Object[] values) throws Exception {
		return dHelper.insertOrUpdateOrDelete(getInsertSql(), values);
	}

	public String getUpdateSql() {
		return " update C_ROLE set ROLE_NAME=?,DESCRIPTION = ?  where ROLE_ID = ? ";
	}

	public String getDeleteSql() {
		return " delete from C_ROLE where ROLE_ID = ? ";
	}

	public String getInsertSql() {
		return " insert into C_ROLE values(?,?,?) ";
	}

	public String getQueryAllSql() {
		return " select * from C_ROLE ";
	}

	public String queryOneByIdSql() {
		return getQueryAllSql() + " where ROLE_ID = ?";
	}

	/**
	 * 获得所有角色列表
	 */
	public List<RoleBean> getByCondition() {
		List<RoleBean> list = new ArrayList<RoleBean>();
		ResultSet rs = dHelper.getRsBySql(getQueryAllSql());
		try {
			while (rs.next()) {
				RoleBean roleBean = new RoleBean();
				roleBean.setRoleID(rs.getString("ROLE_ID"));
				roleBean.setName(rs.getString("ROLE_NAME"));
				roleBean.setDescription(rs.getString("DESCRIPTION"));
				list.add(roleBean);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				dHelper.getDb().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	protected void buildSqlParameters(Map<String, String> map) {
		this.pageSql = this.getQueryAllSql() ;
	}

}
